package com.todod.config;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import com.todod.common.util.RestTemplatUtil;

/**
 * RestTemplate配置类
 */
@Configuration
public class RestTemplateConfig {

	@Bean
	public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
		RestTemplate restTemplate = new RestTemplate(factory);

		restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
		return restTemplate;
	}

	@Bean
	public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
		SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
		factory.setReadTimeout(300000);// 单位为ms
		factory.setConnectTimeout(300000);// 单位为ms
		return factory;
	}

	public static void main(String[] args) {
		RestTemplateConfig restTemplateConfig = new RestTemplateConfig();
		RestTemplate template = new RestTemplate(restTemplateConfig.simpleClientHttpRequestFactory());
		template.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));

		final String url = "http://ap.joywingmau.com.cn:8000/webservices/rest/ERP-WEIXIN/customer_lists/";

		HashMap<String, String> herderMap = new HashMap<String, String>();

		herderMap.put("Content-Type", "application/json");
		herderMap.put("Authorization", RestTemplatUtil.getAuthorization("sysadmin:Jwsys123"));

		String param = "{\"COA_Input\":{\"RESTHeader\":{\"NLSLanguage\":\"SIMPLIFIED CHINESE\"},\"InputParameters\":{\"P_CREATION_DATE_FM\":\"2020-04-01 00:00:00\",\"P_CREATION_DATE_TO\":\"2020-04-14 00:00:00\"}}}";
		HttpEntity<String> httpHeader = RestTemplatUtil.getHeader(param, herderMap);

		String result = template.postForObject(url, httpHeader, String.class);

		System.out.println("--------------");
		System.out.println(result);

	}
}